package happy.town.threeBook;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Scanner;

import static happy.town.threeBook.UserRegister.*;


/**
 * @BelongsProject: threeBook
 * @BelongsPackage: a.happy.town.threeBook
 * @Author: ByHappy  (Email:1300985301@qq.com)
 * @CreateTime: 2023-08-20 12:07
 * @Description: 忘记密码功能——sql版
 * @Version: 1.0
 **/
public class UserForget {

    /**
     * 忘记密码方法
     *
     * @param
     * @author ByHappy
     * @date 12:07 2023/8/20
     **/
    public static void forgetPassword() {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名:");
        String username = sc.next();
        boolean flag = usernameR(username, null, true);
        if (!flag) {
            System.out.println("当前用户:" + username + "未注册!!!请先注册:");
            return;
        }

        // 键盘录入身份证号码和手机号码
        String personID;
        while (true) {
            System.out.println("请确认您的身份证号码:");
            personID = sc.next();
            boolean flag2 = checkPersonID(personID);
            if (flag2) {
                System.out.println("身份证号码格式 OK! 请进行下一步操作");
                break;
            } else {
                System.out.println("您输入的身份证号码格式错误，请重新输入...");
                continue;
            }
        }
        String phoneNumber;
        while (true) {
            System.out.println("请确认您的手机号码:");
            phoneNumber = sc.next();
            boolean flag3 = checkPhoneNumber(phoneNumber);
            if (flag3) {
                break;
            } else {
                System.out.println("手机号码格式错误，请重新输入...");
                continue;
            }
        }

        // 调用findIndex方法判断用户数据是否正确
        boolean index = findIndex(username, personID, phoneNumber);
        // 比较用户对象中的手机号码和身份证号码是否相同
        if (!index) {
            System.out.println("您输入的身份证号码或手机号码输入有误，不能修改密码");
            return;
        }

        //当代码执行到这里，表示所有的数据全部验证成功，直接修改即可
        String password;
        while (true) {
            System.out.println("请输入您的新密码：");
            password = sc.next();
            System.out.println("请再次确认新密码");
            String againPassword = sc.next();
            if (password.equals(againPassword)) {
                System.out.println("两次密码输入一致，OK");
                break;
            } else {
                System.out.println("两次密码输入不一致，请重新输入...");
                continue;
            }
        }

        // sql——————————————————————————————————————————————————————————————切入
        UserDTO userDTO = new UserDTO();
        userDTO.setUsername(username);
        userDTO.setPassword(password);

        // 调用修改方法，判断返回是否成功！(成功便有一条记录)
        int result = update(userDTO);
        if (result != 0) {
            System.out.println("密码修改成功");
        } else {
            System.out.println("密码修改失败");
        }
    }

    /**
     * 判断用户信息是否正确
     *
     * @param username
     * @param personID
     * @param phoneNumber
     * @return int
     * @author ByHappy
     * @date 22:16 2023/8/24
     **/
    private static boolean findIndex(String username, String personID, String phoneNumber) {
        Connection conn = null;
        PreparedStatement pstm = null;
        Statement stat = null;
        ResultSet rs = null;
        UserDTO userDTO = null;
        //最终值(立一个flag,假设拿到的是错误的，)
        boolean flag = false;
        try {
            conn = JDBCUtils.getConnection();

            // 3.获取执行者对象
            stat = conn.createStatement();

            // 4.执行sql语句，并且接收返回的结果集
            String sql = "select * from t_user WHERE username=?";

            pstm = conn.prepareStatement(sql);
            // 3.设置参数
            pstm.setString(1, username);//设置第一个?参数
            //pstm.setString(2, password);//设置第二个?参数
            // System.out.println(sql);
            // 4.执行sql语句，获取结果集
            rs = pstm.executeQuery();
            //5.获取结果集
            if (rs.next()) {
                //6.封装
                userDTO = new UserDTO();
                userDTO.setUsername(rs.getString("username"));
                userDTO.setPersonID(rs.getString("personID"));
                userDTO.setPhoneNumber(rs.getString("phoneNumber"));
            }
            if ((userDTO.getUsername().equals(username) && (userDTO.getPersonID().equals(personID)) && (userDTO.getPhoneNumber().equals(phoneNumber)))) {
                flag = true;
            }


        } catch (Exception e) {

        } finally {
            //6.释放资源
            JDBCUtils.close(conn, stat);
        }
        //将结果返回
        return flag;
    }

    /**
     * 修改密码到mysql
     *
     * @param userDTO
     * @return int
     * @author ByHappy
     * @date 22:39 2023/8/24
     **/
    public static int update(UserDTO userDTO) {
        Connection conn = null;
        Statement stat = null;
        int result = 0;
        try {
            conn = JDBCUtils.getConnection();

            //3.获取执行者对象
            stat = conn.createStatement();

            //4.执行sql语句，并且接收返回的结果集

            String sql = "UPDATE t_user SET password='" + userDTO.getPassword() + "' WHERE username='" + userDTO.getUsername() + "'";
            result = stat.executeUpdate(sql);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //6.释放资源
            JDBCUtils.close(conn, stat);
        }
        //将结果返回
        return result;
    }
}
